$(function(){
    // alert('ok')
    var layer = layui.layer
    var form = layui.form

    var q = {
        pagenum: 1,   // 默认获取第一页的数据
        pagesize: 2,  // 默认每页显示五条数据
        cate_id: '',
        state: '',
    }
    initUser()
    // 获取用户信息
    function initUser() {
        // 发送请求
        $.ajax({
            type: 'get',
            url: '/admin/users',
            data: q,
            success: function(res) {
                // console.log(res);
                if(res.status !== 0) {
                    return layer.msg(res.message)
                }
                // console.log(1111);
                // console.log(template);
                var htmlStr = template('init_user', res)
                $('tbody').html(htmlStr)
                renderPage(res.total)
            }
        })
    }

    // 生成页码
    function renderPage(total) {
        layui.laypage.render({
            elem: 'articlePage',   
            count: total, 
            limit: q.pagesize,   
            curr: q.pagenum,    
            limits: [2, 4, 6, 8, 10],
            layout: ['prev', 'page', 'next','count', 'limit', 'skip'],
            jump: function(obj, first) {
                if(first === true) {
                    return
                } else {
                    q.pagesize = obj.limit
                    q.pagenum = obj.curr   
                }
                initUser()
            }
        })
    }
    //删除用户
    $('tbody').on('click', '.delete', function() {
        var id = $(this).attr('data-id')
        layer.confirm('您确定要删除吗?', {icon: 3, title:'提示'}, function(index){
            //do something
            
            var btns = $('.delete').length
            $.ajax({
                type: 'DELETE',
                url: '/admin/users/' + id,
                success:function(res) {
                    // console.log(res);
                    if(res.status !== 0) {
                        return layer.msg(err.message)
                    }
                    if (btns === 1) {
                        // q.pagenum--;
                        // 如果pagenum已经是第1页，就不用-1了
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1
                    }
                    initUser()
                }
            })
            layer.close(index);
        });
    })


    // 点击编辑跳转
    $('tbody').on('click', '#edit', function() {
        var id = $(this).attr('data-id')
        location.href = './edit.html';
        localStorage.setItem('id', id)
    })


    

    // 修改密码
    $('tbody').on('click', '.resetpwd', function() {
        var index = layer.open({
            content: $('#repwd-form-tpl').html()
        })
        form.verify({
            pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ] ,
            repwd: function(value, item) {
                // value是确认密码框的值(谁使用该规则就是谁的值)
                var password = $('[name=password]').val()
                if(value !== password) {
                    return '两次密码不一致'
                }
            }
        }); 


        var id = $(this).attr('data-id')
        $('#repwd-form').submit(function(e) {
            
            e.preventDefault()
            var password = $('[name=password]').val()
            console.log(password);
            $.ajax({
                type: 'PUT',
                url: '/admin/users/' + id,
                data: 'password',
                success: function(res) {
                    // console.log(res);
                    if(res.status !== 0 ) {
                        return layer.msg(res.message)
                    } 
                    layer.msg(res.message)
                    layer.close(index);
                }
            })
        })
        
    })


    
})